// pages/index/BeautyKilling/BeautyKilling.js
var url = wx.getStorageSync('url')
var userId = wx.getStorageSync('userId')
var openid = wx.getStorageSync('openid')
var time_ss = wx.getStorageSync('time_ss')
var shopid = wx.getStorageSync('shopid')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    danmuList: [
      {
        text: '第 1s 出现的弹幕',
        color: '#ff0000',
        fontsize: '20rpx',
        time: 5
      },
      {
        text: '第 3s 出现的弹幕',
        color: '#ff00ff',
        time: 30
      }],
    countDownList: [],
    actEndTimeList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')
    let goodsList = [
      { actEndTime: '2019/08/01 10:00:43' },
      { actEndTime: '2019/08/02 16:00:43' },
    ]
    let endTimeList = [];
    // 将活动的结束时间参数提成一个单独的数组，方便操作
    goodsList.forEach(o => { endTimeList.push(o.actEndTime) })
    this.setData({ actEndTimeList: endTimeList });
    // 执行倒计时函数
    this.countDown();
    wx.showLoading({
      title: '加载中',
    })

    var that = this
    wx.request({
      url: url+'sharkActivity/fetchLayout',
      data: {
        "page": 1,
        "limit": 10,
        "type": 1,
        "seq": "232312313",
        "sysVer": "0.0.1",
        "sendTime": time_ss,
        "userId": userId,
        "resId": "0000",
        "privId": "0000",
        "platform": 2,
        "shopId": shopid,
        "userType": 1,
        "source": 1,
        "content": {
          "shopId": shopid,
          "actType": 2
        }
      },
      method: "POST",
      header: { "Content-Type": "application/JSON" },
      success: function (e) {
        wx.hideLoading()
        var data = e.data.content
        console.log(e.data)
        that.setData({
          datas: data
        })
      }
    })

  },
  timeFormat(param) {//小于10的格式化函数
    return param < 10 ? '0' + param : param;
  },
  countDown() {//倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime();
    let endTimeList = this.data.actEndTimeList;
    let countDownArr = [];

    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(o => {
      let endTime = new Date(o).getTime();
      let obj = null;
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: day,
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
      } else {//活动已结束，全部设置为'00'
        obj = {
          day: '0',
          hou: '00',
          min: '00',
          sec: '00'
        }
      }
      countDownArr.push(obj);
    })
    // 渲染，然后每隔一秒执行一次倒计时函数
    // console.log(countDownArr[0])
    // console.log(countDownArr[1])
    this.setData({
      times1: countDownArr[0],
      times2: countDownArr[1]

    })
    setTimeout(this.countDown, 1000);
  },
  goto_details:function(e){
    var wareId = e.currentTarget.id
    var activityId = e.currentTarget.dataset.activityid
    wx.navigateTo({
      url: 'kill_details/kill_details?wareId=' + wareId + '&activityId=' + activityId,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})